{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Building histograms using Rectangles and PolyCollections\n",
    "\n",
    "\n",
    "Using a path patch to draw rectangles.\n",
    "The technique of using lots of Rectangle instances, or\n",
    "the faster method of using PolyCollections, were implemented before we\n",
    "had proper paths with moveto/lineto, closepoly etc in mpl.  Now that\n",
    "we have them, we can draw collections of regularly shaped objects with\n",
    "homogeneous properties more efficiently with a PathCollection.  This\n",
    "example makes a histogram -- its more work to set up the vertex arrays\n",
    "at the outset, but it should be much faster for large numbers of\n",
    "objects\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD8CAYAAAB6paOMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAD85JREFUeJzt3XGsXnV9x/H3x5biEhZFMZlpC5TYLdbpRLG4kKGbiHUj7ZJhLIkbLCzN3Mi2mGXDmUBW/5GZbMs2FulmE+fmKuJi7kgJYYLbHwbsFRFtGXrpBG5qwqQMZ3CQwnd/PIf58Oy53HN7n9vnIb/3K3nSc37n9zv3e3+X537uOec5h1QVkqR2vWzaBUiSpssgkKTGGQSS1DiDQJIaZxBIUuMMAklqXK8gSLIjyYNJFpJcO2b7h5IcSXJ/ki8mOWdo27NJ7utec5MsXpK0elnuPoIk64BvAe8GFoFDwBVVdWSoz88D91TVU0k+CLyzqt7fbftBVZ2xVt+AJGl1+hwRbAcWqupoVT0DHAB2DXeoqruq6qlu9W5g02TLlCStlfU9+mwEHh1aXwQufJH+VwO3Da2/PMk8cAL4WF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tMXqRNmYE67UwT3AY8Bd1TVkvNZVSeA5x//Mks1wmy81/8c+APguSW2T30uO8vVCSucz2kFQZ9HT/wzcG5VvQn4F36UxLOk1yM0ZsC9wDlV9TPAXwJfmGYxSc4APg/8XlV9f3TzmCFTmdNl6pyJOa2qZ6vqzQzu2t+e5KdHukx9PnvUOPX3epLLgMeq6qsv1m1M2ymdy551rng+pxUEyz56oqoer6qnu9W/Ad56impbiZfEIzSq6vvPH55X1UHgtCRnTaOWJKcx+OX6D1X1T2O6zMScLlfnLM1pV8N/AV9i8Lj3YUs9/uWUW6rGGXmvXwTsTPIdBqeqfyHJ34/0mYW5XLbOk5nPaQXBso+tGDkvvJPBedpZMwf8WvdJl7cDT1bVd6dd1KgkP/H8ucwk2xn83B+fQh1hcBf6A1X1p0t0m/qc9qlzFuY0yWuSvLJb/jHgEuDfR7ot9fiXmalxFt7rVfXhqtpUVecy+H10Z1V9YKTbVOcS+tV5MvPZ5+mjE1f9HlvxO0l2Mng0xXEGV8JPqST/yODTIWclWQSuZ3Cxi6r6BHCQwadcFoCngF8/1TX2rPNy4INJTgA/BHaf6v+AOxcBvwp8oztnDPBHwNlDtc7CnPapcxbm9LXApzL4nz29DLi5qm5Nj8e/zFiNU3+vL2XG5nJJq51PHzEhSY3zzmJJapxBIEmNMwgkqXEGgSQ1ziCQpMYZBJLUOINAkhr3v6F38f5JSqqGAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.patches as patches\n",
    "import matplotlib.path as path\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "# Fixing random state for reproducibility\n",
    "np.random.seed(19680801)\n",
    "\n",
    "\n",
    "# histogram our data with numpy\n",
    "\n",
    "data =[1,2,3,4]\n",
    "n, bins = np.histogram(data, [1,2,3,4,5],density=True)\n",
    "\n",
    "# get the corners of the rectangles for the histogram\n",
    "left = np.array(bins[:-1])-0.5\n",
    "right = np.array(bins[1:])-0.5\n",
    "bottom = np.zeros(len(left))\n",
    "top = bottom + n\n",
    "\n",
    "\n",
    "# we need a (numrects x numsides x 2) numpy array for the path helper\n",
    "# function to build a compound path\n",
    "XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T\n",
    "\n",
    "# get the Path object\n",
    "barpath = path.Path.make_compound_path_from_polys(XY)\n",
    "\n",
    "# make a patch out of it\n",
    "patch = patches.PathPatch(barpath)\n",
    "ax.add_patch(patch)\n",
    "\n",
    "# update the view limits\n",
    "ax.set_xlim(left[0], right[-1])\n",
    "ax.set_ylim(bottom.min(), top.max())\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
